/**
 * @project spring-threadpool-async
 * @description
 * @errorcode SystemException</ br>
 * @author 2022 15:16 huzhengyang Create 1.0 <br>
 * @copyright ©2022-2023
 */

package com.teradata.async.controller;

import com.huaweicloud.sdk.ief.v1.model.ConfigMapResp;
import com.huaweicloud.sdk.ief.v1.model.EdgeNodeResp;
import com.huaweicloud.sdk.ief.v1.model.ListConfigMapsResponse;
import com.huaweicloud.sdk.ief.v1.model.ListEdgeNodesResponse;
import com.teradata.async.dto.Result;
import com.teradata.async.dto.ResultUtil;
import com.teradata.async.service.AsyncService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/**
 * @Author HO
 * @Package com.zhitu.thread.controller
 * @Description
 * @Date 2020/8/5 11:03
 * @Version 1.0.0
 */
@RestController
@Slf4j
public class AsyncController {

    protected static final Logger LOGGER = LoggerFactory.getLogger(AsyncController.class);

    @Autowired
    private AsyncService asyncService;

    @RequestMapping("/")
    public String submit() {
        LOGGER.info("start submit");
        //调用service
        this.asyncService.executeAsync();
        //调用结束 打印日志
        LOGGER.info("end submit");

        return "success";
    }


    @GetMapping(path = "/nodelist", produces = MediaType.APPLICATION_JSON_VALUE)
    public Callable<Result<List<EdgeNodeResp>>> getListEdgeNodes() {
        String requestRetrieveThreadName = Thread.currentThread().getName();
        log.info("retrieve request thread name is :" + requestRetrieveThreadName);
        return new Callable<Result<List<EdgeNodeResp>>>() {
            @Override
            public Result<List<EdgeNodeResp>> call() throws Exception {
                try {
                    CompletableFuture<ListEdgeNodesResponse> listEdgeNodesResponseCompletableFuture = asyncService.listEdgeNodesAsync();
                    List<EdgeNodeResp> nodes = listEdgeNodesResponseCompletableFuture.get().getNodes();
                    String responseThreadName = Thread.currentThread().getName();
                    log.info("response data thread name is :" + responseThreadName);
                    return ResultUtil.success(nodes);
                } catch (InterruptedException | ExecutionException e) {
                    return ResultUtil.error(e);
                }
            }
        };
    }

    @GetMapping(path = "/configmap", produces = MediaType.APPLICATION_JSON_VALUE)
    public Callable<Result<List<ConfigMapResp>>> getListConfigMaps() {
        String requestRetrieveThreadName = Thread.currentThread().getName();
        log.info("retrieve request thread name is :" + requestRetrieveThreadName);
        return () -> {
            try {
                CompletableFuture<ListConfigMapsResponse> listConfigMapsResponseCompletableFuture = asyncService.listConfigMapsAsync();
                List<ConfigMapResp> configmaps = listConfigMapsResponseCompletableFuture.get().getConfigmaps();
                String responseThreadName = Thread.currentThread().getName();
                log.info("response data thread name is :" + responseThreadName);
                return ResultUtil.success(configmaps);
            } catch (InterruptedException | ExecutionException e) {
                return ResultUtil.error(e);
            }
        };
    }
}

